// input输入动画
const inputs = document.querySelectorAll(".input")

function addcl(){
    let parent = this.parentNode.parentNode;
    parent.classList.add("focus")
}

function remcl(){
    let parent = this.parentNode.parentNode;
    if(this.value === ""){
        parent.classList.remove("focus")
    }
}

inputs.forEach(input => {
    input.addEventListener("focus",addcl);
    input.addEventListener("blur",remcl);
})
// dom操作
// 选择元素
var layer = $('.layer');
var  layer_info = $('.layer div div');

// 表单提交逻辑
$('.btn').click(function () {
    const info = $('form input');
    console.log(info[0].value, info[1].value);
    $.ajax({
        type: 'POST',
        url: '/api/users/emailCode',
        data: {
            'user': {
                'username': info[0].value,
                'email':info[1].value
            }
        },
        success: function (data) {
            // console.log(data);
            layer_info.attr('id', 'success');
            layer_info.append('<i class="fas fa-smile"><span> 验证码发送成功</span></i>');
            layer.fadeIn(600);
            setTimeout(function () { layer.fadeOut(450); }, 1500);
            // 将邮箱地址存入storage
            localStorage.setItem('email',info[1].value);
            setTimeout(function () { window.location.href = '/resetpwd.html'; }, 2300);
        },
        error: function (err) {
            console.log(err.responseText);
            // 失败
            layer_info.attr('id', 'error');
            errorMessage = err.responseText.match(/[\u4e00-\u9fa5]+/g);
            layer_info.append(`<i class="fas fa-tired"><span> 注册失败：${errorMessage}</span></i>`);
            layer.fadeIn(600);
            setTimeout(function () { layer.fadeOut(450); }, 1500);
            // 清楚layer_info的id和内容
            setTimeout(() => {layer_info.removeAttr('id');layer_info.empty();}, 6000);
        }
    });
});
